package com.ngc.mall.logic;

import com.ngc.mall.bo.SendResponse;
import com.ngc.mall.bo.SendToOneRequest;
import com.ngc.mall.bo.SendToUserRequest;
import com.ngc.mall.common.configure.WebSocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 *
 * 消息发送给指定用户消息处理器
 * @author: chenxiaolin
 * @date 2023/11/7 14:43
**/
@Component
public class SendToOneHandler implements MessageHandler<SendToOneRequest> {

    @Override
    public void execute(Session session, SendToOneRequest message) {
        // 这里，假装直接成功
        SendResponse sendResponse = SendResponse.builder().msgId(message.getMsgId()).code(0).build();
        WebSocketUtil.send(session, SendResponse.TYPE, sendResponse);

        // 创建转发的消息
        SendToUserRequest sendToUserRequest =SendToUserRequest.builder().content(message.getContent()).msgId(message.getMsgId()).build();
        // 广播发送
        WebSocketUtil.send(message.getToUser(), SendToUserRequest.TYPE, sendToUserRequest);
    }

    @Override
    public String getType() {
        return SendToOneRequest.TYPE;
    }
}